package com.company.project.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.List;

import com.company.project.common.utils.DataResult;

import com.company.project.entity.SysAreaEntity;
import com.company.project.service.SysAreaService;


/**
 * 地区码表
 *
 * @author YangYulin
 * @email YangYulinit@163.com
 * @date 2020-07-30 17:41:13
 */
@Controller
@Slf4j
@RequestMapping("/")
@Api(tags = "系统管理-城市地区码")
public class SysAreaController {
    @Autowired
    private SysAreaService sysAreaService;


    /**
     * 跳转到页面
     */
    @GetMapping("/index/sysArea")
    public String sysArea() {
        return "sysarea/list";
    }

    @ApiOperation(value = "新增")
    @PostMapping("sysArea/add")
    @RequiresPermissions("sysArea:add")
    @ResponseBody
    public DataResult add(@RequestBody SysAreaEntity sysArea) {
        sysAreaService.save(sysArea);
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("sysArea/delete")
    @RequiresPermissions("sysArea:delete")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids) {
        sysAreaService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("sysArea/update")
    @RequiresPermissions("sysArea:update")
    @ResponseBody
    public DataResult update(@RequestBody SysAreaEntity sysArea) {
        sysAreaService.updateById(sysArea);
        return DataResult.success();
    }

    @ApiOperation(value = "查询分页数据")
    @PostMapping("sysArea/listByPage")
    @RequiresPermissions("sysArea:list")
    @ResponseBody
    public DataResult findListByPage(@RequestBody SysAreaEntity sysArea) {
        Page page = new Page(sysArea.getPage(), sysArea.getLimit());
        QueryWrapper queryWrapper = new QueryWrapper();
        //查询条件示例
        if(!StringUtils.isEmpty(sysArea.getId())){
            queryWrapper.eq("id", sysArea.getId());
        }
        if (!StringUtils.isEmpty(sysArea.getAreaName())) {
            queryWrapper.like("area_name", sysArea.getAreaName());
        }
        if (null != sysArea.getLevel()) {
            queryWrapper.like("level", sysArea.getLevel());
        }
        if (!StringUtils.isEmpty(sysArea.getAreaName())) {
            queryWrapper.like("area_name", sysArea.getAreaName());
        }
        queryWrapper.orderByAsc("area_code");
        IPage<SysAreaEntity> iPage = sysAreaService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }

    @ApiOperation(value = "查询列表")
    @PostMapping("sysArea/list")
    @ResponseBody
    public DataResult findList(@RequestBody SysAreaEntity sysArea){
        QueryWrapper queryWrapper = new QueryWrapper();
        log.info(JSONObject.toJSONString(sysArea));
        if (null != sysArea.getAreaCode()) {
            queryWrapper.eq("area_code", sysArea.getAreaCode());
        }
        if (null != sysArea.getLevel()) {
            queryWrapper.eq("level", sysArea.getLevel());
        }
        if (!StringUtils.isEmpty(sysArea.getParentId())) {
            queryWrapper.eq("parent_id", sysArea.getParentId());
        }
        List<SysAreaEntity> sysAreaEntities = sysAreaService.list(queryWrapper);
        return DataResult.success(sysAreaEntities);
    }
}
